package edu.mit.media.fv.blocks
{
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.icons.*;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.*;

	public class Block extends Sprite
	{
		
		protected static var SHADOW:uint = 0x000000;
		private var glow:GlowFilter = new GlowFilter(0x000000,0.4,15,15);
		private var deepglow:GlowFilter = new GlowFilter(0x000000,0.8,30,30);
		private var dropShadow:DropShadowFilter = new DropShadowFilter();
		//var glow:GlowFilter = new GlowFilter(0x8DD613,0.2,15,15);
		
		protected var controller:FluidSpace = null;
		protected var _icon:DisplayObject = null;
		private var _blockContainer:BlockContainer = null;
		private var _color:uint;
		private var _radius:int;
		protected var _label:TextField;
		private var _model:Model;
		private var _isDragging:Boolean = false;
		public var popupPosition:String = AQBlockPopup.AROUND;
		//private var _hitArea:Sprite = new Sprite();
		protected var menuItems:Array;
		//private var popup:BlockPopup;
		public function Block(model:Model, controller:FluidSpace, icon:DisplayObject = null, text:String = "", color:uint = 0xFFFFFF){
			super();
			this._model = model;
			this.controller = controller;
			this.doubleClickEnabled = true;
			var format:TextFormat = new TextFormat();
			format.align = "center";
			format.bold = true;
			format.font = "Arial";
			format.size = 14;
			this._label = new TextField();
			this._label.defaultTextFormat = format;
			this._label.type = TextFieldType.DYNAMIC;
			this._label.selectable = false;
			this._label.doubleClickEnabled = true;
			this._label.mouseEnabled = true;
			this._label.text = text;
			this._label.height = 20;
			
			this._color = color;
			
			this.addChild(this._label);
			this.filters = [glow];
			
			//this.addChild(this._hitArea);
			//_hitArea.doubleClickEnabled = true;
			//this.hitArea = this._hitArea;
			//this.addEventListener(MouseEvent.CLICK, processMouseClicked);
			
			var info:DisplayObject = new AQInfoIcon();
			info.addEventListener(MouseEvent.CLICK, viewBlock);
			var thumbsup:DisplayObject = new AQThumbsUpIcon();
			thumbsup.addEventListener(MouseEvent.CLICK, markGood);
			var thumbsdown:DisplayObject = new AQThumbsDownIcon();
			thumbsdown.addEventListener(MouseEvent.CLICK, markBad);
			
			
			menuItems = [thumbsup, info, thumbsdown];
			//this.popup = new BlockPopup(viewBlock, markGood, markBad);
			//this.addChild(this.popup);
			
			this.icon = icon;
			
			this.draw();
			
		}
		private function markBad(event:Event):void{
			this.model.markBad();
		}
		private function markGood(event:Event):void{
			this.model.markGood();
		}
		
		private function viewBlock(event:Event):void{
            trace('Block.as: viewBlock')
			this.model.openModel();
		}
		//setters and getters
		public function get icon():DisplayObject{
			return this._icon;
		}
		public function set icon(icon:DisplayObject):void{
			if(this._icon != null){
				this.removeChild(this._icon);
			}
			this._icon = icon;
			if(icon != null){
				this.addChild(icon);
			}
		}
		public function get model():Model{
			return this._model;
		}
		public function get blockContainer():BlockContainer{
			return this._blockContainer;
		}
		public function set blockContainer(container:BlockContainer):void{
			this._blockContainer = container;
		}
		public function get color():uint{
			return this._color;
		}
		public function set color(color:uint):void{
			this._color = color;
			this.draw();
		}
		public function get radius():int{
			return this._radius;
		}
		public function set radius(radius:int):void{
			this._radius = radius;
			this._label.x = 0;
			this._label.y = this.diameter*5/8;
			this._label.width = radius*2;
			this.draw();
			if(this._icon != null){
				this._icon.width = this.radius;
				this.icon.height = this.radius;
				icon.x = this.radius/2;
				icon.y = this.radius/4;
			}
		}
		public function get diameter():int{
			return this.radius*2;
		}
		public function set diameter(diameter:int):void{
			this.radius = diameter/2;
		}
		public function get label():String{
			return this._label.text;
		}
		public function set label(text:String):void{
			this._label.text = text;
		}
		public override function toString():String{
			return "Block "+this.label+": "+super.toString();
		}
		
		//dragging
		public var dragPoint:Point = new Point(0,0);
		public function startDragging(event:MouseEvent):void{
			_isDragging = true;
			this.logDragPoint();
			FluidEventHandler.dispatchEvent(new BlockEvent(BlockEvent.BLOCK_DRAGGED, this, null));
		}
		public function logDragPoint():void{
			this.dragPoint.x = this.x;
			this.dragPoint.y = this.y;
		}
		public function stopDragging(event:MouseEvent):void{
			//this.filters = [glow];
			FluidEventHandler.dispatchEvent(new BlockEvent(BlockEvent.BLOCK_DROPPED, this, null));
			_isDragging = false;
		}
		
		//reforming
		
		public function draw():void{
			
		}
		
		public function snapBack():void{
			if(this.blockContainer != null){
				this.x =this.dragPoint.x;
				this.y = this.dragPoint.y;
				this.blockContainer.addBlock(this);
			}
		}
		public function selfDestruct():void{
			if(this.blockContainer != null){
				this.blockContainer.removeBlock(this);
			}
			if(this.parent != null){
				this.parent.removeChild(this);
			}
		}
		public function openView():void{
			this.model.openModel();
		}
		public function moveBlock(newBlockContainer:BlockContainer):void{
			this.stopDrag();
			var stagePoint:Point = this.localToGlobal(new Point(0, 0));
			var containerPoint:Point = newBlockContainer.globalToLocal(new Point(stagePoint.x,stagePoint.y));
			this.x = Math.max(0, Math.min(containerPoint.x, newBlockContainer.containerWidth-this.diameter));
			this.y = Math.max(0, Math.min(containerPoint.y, newBlockContainer.containerHeight-this.diameter));
			if(this.blockContainer != null){
				this.blockContainer.removeBlock(this);
			}
			trace(this.x+", "+this.y);
			newBlockContainer.addBlock(this);
			if(Math.abs(this.dragPoint.x - this.x)<5){
				if(Math.abs(this.dragPoint.y - this.y)<5){
					if(_isDragging){
						AQBlockPopup.show(this._radius, this._radius, this.popupPosition, this, menuItems);
					}
				}
			}
		}
	}
}